<?php
	session_start();
	if(!isset($_SESSION['user'])){
		header("Location: admin_login.php");
		exit;
	}
	
	require_once 'init.php';
	require_once 'application/user_dao.php';
	
	if(isset($_REQUEST['id_section']) && !empty($_REQUEST['id_section'])){
		$section = new Section($_REQUEST['id_section'],
							   $_REQUEST['name_section'],
							   $_REQUEST['message'],
							   null,
							   "menu_section.php?id=".$_REQUEST['id_section'],
							   $_REQUEST['active'],
							   null,
							   (isset($_REQUEST['page_title'])) && !empty($_REQUEST['page_title'])?$_REQUEST['page_title']:null);
		
		$section_manager = new Section_manager($conn);
		
		$section_manager->update($section);
		
		header('Location: admin.php');
		exit();
	}else if(!isset($_REQUEST['id']) || empty($_REQUEST['id'])){
		header('Location: admin.php');
		exit();
	}
	
	$user = unserialize($_SESSION['user']);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>longtailcap.com</title>
		<link rel="stylesheet" type="text/css" href="resources/css/style.css" />
		
		<!-- Skin CSS file -->
		<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.0r4/build/assets/skins/sam/skin.css">
		<!-- Utility Dependencies -->
		<script src="http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js"></script> 
		<script src="http://yui.yahooapis.com/2.8.0r4/build/element/element-min.js"></script> 
		<!-- Needed for Menus, Buttons and Overlays used in the Toolbar -->
		<script src="http://yui.yahooapis.com/2.8.0r4/build/container/container_core-min.js"></script>
		<!-- Source file for Rich Text Editor-->
		<script src="http://yui.yahooapis.com/2.8.0r4/build/editor/editor-min.js"></script>
		
	</head>
	<body class="yui-skin-sam">
<script type="text/javascript">
var editor = new YAHOO.widget.Editor('message', {
	handleSubmit: true
});
editor._defaultToolbar.titlebar = false;
editor._defaultToolbar.buttonType = 'advanced';
editor.render();
</script>
		<div class="main" style="text-align: center; padding: 5px; font-size: 10pt">
			<div style="text-align: right">
				<div style="float: right; padding-right: 10px">
					<a href="logout.php">logout</a>
				</div>
				<div style="float: right; padding-right: 10px"><?php echo $user->getNameUser(); ?></div>
				<div style="clear: both"></div>
			</div>
			<h1>Site Administrator Editor</h1>
			<?php
				$section_manager = new Section_manager($conn);
				$section = $section_manager->getSection($_REQUEST['id'])
			?>
			<form action="section_edition.php" method="post" id="form1" name="form1">
				<table align="center" cellpadding="0" cellspacing="5" border="0">
					<tr valign="top">
						<td style="text-align: left; width: 20%; padding-right: 10px; font-weight: bold">Id:</td>
						<td style="text-align: left; width: 80%">
							<?php echo $section->getIdSection(); ?>
							<input type="hidden" name="id_section" value="<?php echo $section->getIdSection(); ?>" />
						</td>
					</tr>
					<tr valign="top">
						<td style="text-align: left; padding-right: 10px; font-weight: bold">Name:</td>
						<td style="text-align: left"><input type="text" name="name_section" value="<?php echo $section->getNameSection(); ?>" style="width: 200px" /></td>
					</tr>
					<tr valign="top">
						<td style="text-align: left; padding-right: 10px; font-weight: bold">Page Title:</td>
						<td style="text-align: left"><input type="text" name="page_title" value="<?php echo $section->getPageTitle(); ?>" style="width: 200px" /></td>
					</tr>
					<tr valign="top">
						<td style="text-align: left; padding-right: 10px; font-weight: bold">Active:</td>
						<td style="text-align: left">
							<input type="radio" name="active" value="1" <?php echo ($section->getActive() == 1?'checked="checked"':''); ?>/>Yes
							<input type="radio" name="active" value="0" <?php echo ($section->getActive() == 0?'checked="checked"':''); ?>/>No
						</td>
					</tr>
					<tr>
						<td style="text-align: left; padding-right : 10px; font-weight: bold">Content:</td>
						<td></td>
					</tr>
					<tr>
						<td colspan="2">

<!-- yui begins -->		
<textarea id="message" name="message" rows="20" cols="75"><?php echo $section->getContent(); ?></textarea> 
<!-- yui ends -->
					
						</td>
					</tr>
					<tr>
						<td colspan="2"><input type="submit" value="submit" /></td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>